package com.chinafree.intercepter;

import com.chinafree.permission.service.PathServie;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
public class JWTInterceptor implements HandlerInterceptor {


    @Autowired
    private PathServie pathServie;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String requestPath = request.getRequestURI();
        log.info("requestPath;{}",requestPath);

        String token = request.getHeader("token");

        return true;

//        try {
//            final DecodedJWT tokenInfo = JWTUtils.getTokenInfo(token);
//            Claim uid = tokenInfo.getClaims().get(JWTValue.USER_ID.getValue());
//            String method = request.getMethod();
//            boolean permission = pathServie.hasPermission(uid.toString(), requestPath, method);
//            if (!permission) {
//                String pathInfo = pathServie.getPathInfo(uid.toString(), requestPath, method);
//                BaseResponse res = new BaseResponse(ResponseCodeEnum.FORBIDDEN,
//                        "当前用户无访问权限" + "\n" + pathInfo);
//                response.setContentType(MediaType.APPLICATION_JSON_VALUE);
//                response.getOutputStream().write(JsonUtils.writeValueAsString(res).getBytes());
//            }
//            return true;
//        }catch (SignatureVerificationException e){
//            response.sendError(403,"无效签名");
//            e.printStackTrace();
//            return false;
//        }catch (AlgorithmMismatchException e){
//            response.sendError(403,"token过期");
//            e.printStackTrace();
//            return false;
//        }catch (Exception e){
//            response.sendError(403,"token无效");
//            e.printStackTrace();
//            return false;
//        }
    }
}
